<template>
  <div>
    <myheader @back='fn'>京东登陆注册</myheader>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="name"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="psw"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button
          round
          block
          type="info"
          native-type="submit"
        >
            登录
        </van-button>
      </div>
    </van-form>
  </div>
</template>
<script>
// @ is an alias to /src
import myheader from "@/common/header";
import {login} from '@/api/api'
export default {
  name: "Login",
  data() {
    return {
        username: '',
        password: '',
    };
  },
  methods: {
    fn() {
      console.log(666);
      this.$router.push("/list");
    },
    onSubmit(values) {
      console.log('submit', values);
      login(values).then(data=>{
          console.log(data)
          this.$store.commit('changeUserInfo',{
              name:this.username,
              token:data.token
          })
          localStorage.setItem('jd_user',JSON.stringify({
              name:this.username,
              token:data.token
          }))
          this.$router.replace('/')
      })
    },
  },
  components: {
    myheader
  }
};
</script>
<style lang="less">
</style>